package cn.jinshengda.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.jinshengda.entity.*;
import cn.jinshengda.mapper.*;
import cn.jinshengda.service.*;
import cn.jinshengda.utils.DateUtil;
import cn.jinshengda.utils.GetAgeUtil;
import cn.jinshengda.utils.YearUtils;
import cn.jinshengda.vo.MonthAndCount;
import cn.jinshengda.vo.EmployeeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-01-08
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private DegreeMapper degreeMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private DailyPaperMapper dailyPaperMapper;
    @Autowired
    private MonthlyPaperMapper monthlyPaperMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    /*
     * @Author Lxy
     * @Description //TODO 根据学历查询员工
     * @Param education 学历 pageNow 当前页 rows 每页显示条数 workYearsName 工作年限
     * @return 返回员工信息
    */
    @Override
    public Map<String, Object> searchEducation(Integer pageNow,Integer rows,String education,String workYearsName) {
        IPage<Employee> iPage=new Page<>(pageNow,rows);
        QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
        //通过学历名查询学历表学历
        if (!ObjectUtils.isEmpty(education))queryWrapper.eq("education",education);
        if (!ObjectUtils.isEmpty(workYearsName))queryWrapper.eq("worktime",workYearsName);
        //查询出对应学历
        iPage=employeeMapper.selectPage(iPage,queryWrapper);
        List<Employee> list = iPage.getRecords();
        for (Employee employee : list) {
            String age = GetAgeUtil.evaluate(employee.getNumberId());
            employee.setAge(Integer.parseInt(age));
            //根据岗位查
            Position position = positionMapper.selectById(employee.getPositionId());
            employee.setPositionName(position.getName());
            //根据学历查
            Degree degree2 = degreeMapper.selectById(employee.getDegreeId());
            employee.setDegree(degree2);
            //map.put("education",degree);

            QueryWrapper<Resume> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("employee_id",employee.getId());
            List<Resume> resumes = resumeMapper.selectList(queryWrapper1);
            employee.setResume(resumes);
            //map.put("resume",resume);

            QueryWrapper<DailyPaper> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.eq("employee_id", employee.getId());
            List<DailyPaper> dailyPapers = dailyPaperMapper.selectList(queryWrapper3);
            employee.setDailyPapers(dailyPapers);
            //map.put("dailyPapers",dailyPapers);

            QueryWrapper<MonthlyPaper> queryWrapper4=new QueryWrapper<>();
            queryWrapper4.eq("employee_id", employee.getId());
            List<MonthlyPaper> monthlyPapers = monthlyPaperMapper.selectList(queryWrapper4);
            employee.setMonthlyPapers(monthlyPapers);
            //map.put("monthlys",monthlyPapers);
            Department department = departmentMapper.selectById(employee.getDepartmentId());
            employee.setDepartment(department);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("data",list);
        map.put("page",pageNow);
        Integer count = employeeMapper.queryCount(education,workYearsName);
        int pageNum=count%rows==0?count/rows:count/rows+1;
        map.put("pageNum",pageNum);
        System.out.println(count);
        return map;
    }

    @Override
    public Integer queryByDepartmentCount(Integer id) {
        return employeeMapper.queryByDepartmentCount(id);
    }


    @Override
    public Map<String, Object> queryByMonthEmployee() {
        Map<String,Object> map=new HashMap<>();
        List<String> month = YearUtils.getMonth();

        for (String s : month) {
            System.out.println(s);
        }

        String startMonth = month.get(0);
        String endMonth = month.get(11);
        System.out.println(startMonth);
        System.out.println(endMonth);
        List<Integer> manCount=new ArrayList<>();
        List<Integer> womanCount=new ArrayList<>();
        List<MonthAndCount> man = employeeMapper.queryByMonthCount("男", startMonth, endMonth);
        List<MonthAndCount> woman=employeeMapper.queryByMonthCount("女", startMonth, endMonth);
        List<Employee> employeeMan = employeeMapper.queryByMonthEmployee("男", startMonth, endMonth);
        List<Employee> employeeWoman = employeeMapper.queryByMonthEmployee("女", startMonth, endMonth);
        for (int i=1;i<=12;i++){
            boolean flag=false;
            for (MonthAndCount monthAndCount : man) {
                if (monthAndCount.getMonth().equals(i+"")){
                    manCount.add(monthAndCount.getCount());
                    flag=true;
                }
            }
            if(flag == false){
                manCount.add(0);
            }
        }
        for (int i=1;i<=12;i++){
            boolean flag=false;
            for (MonthAndCount monthAndCount : woman) {
                if (monthAndCount.getMonth().equals(i+"")){
                    womanCount.add(monthAndCount.getCount());
                    flag=true;
                }
            }
            if(flag == false){
                womanCount.add(0);
            }
        }
        map.put("month",month);
        map.put("manCounts",manCount);
        map.put("womanCounts",womanCount);
        map.put("employeeMan",employeeMan);
        map.put("employeeWoman",employeeWoman);
        return map;
    }

    /*
    导出excel文件
     */
    @Override
    public void export(int page) {
        int size=3;
        Page<Employee> page1 = new Page<>(page, size);
        employeeMapper.selectPage(page1, null);
        List<Employee> list = page1.getRecords();
        //List<Employee> list = employeeMapper.queryByPage((page - 1) * size, size);

        List<EmployeeVo> list1 = new ArrayList<>();
        for (Employee employee : list) {
            System.out.println(employee+"********************************************");
            EmployeeVo employeeVo = new EmployeeVo();
            BeanUtils.copyProperties(employee, employeeVo);
            employeeVo.setIdcard(employee.getNumberId());
            employeeVo.setBorn_date(employee.getBirthday());
            employeeVo.setPolitical_status(employee.getOutlook());
            employeeVo.setEthnicity(employee.getNameFamily());
            employeeVo.setEmergency_call(employee.getEmergencyContactPhone());
            employeeVo.setAge(DateUtil.getAge(employeeVo.getBorn_date()));
            employeeVo.setPositionName(positionMapper.selectById(employeeVo.getPositionId()).getName());
            employeeVo.setDepartment(departmentMapper.selectById(employeeVo.getDepartmentId()).getName());

//            QueryWrapper<Resume> queryWrapper=new QueryWrapper<>();
//            queryWrapper.eq("employee_id", employeeVo.getId());
//            employeeVo.setResume(resumeMapper.selectList(queryWrapper));
            list1.add(employeeVo);
            System.out.println(employeeVo+"-------------------------------------");
        }

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("员工列表","员工"),EmployeeVo.class, list1);
        try {
            workbook.write(new FileOutputStream(new File("E:/jsdEmp.xls")));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
